<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑':'添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <AddorEdit
          v-if="showDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="close"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from '@/views/departments/deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制弹层
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        const res = await getDepartments()
        console.log('res', res)
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          return { id: item.id, name: item.name, pid: item.pid, code: item.code }
        })
        // console.log(this.originList)
        this.list = tranListToTreeData(res.data.depts)
        // console.log('this.list', this.list)
      } catch (error) {
        console.log(error)
      }
    },
    // 点击添加子组件
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 子传父 关闭弹层 重新渲染页面
    hSuccess() {
      this.showDialog = false
      this.loadDepartment()
    },
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
    },
    // 删除
    hDel(id) {
      this.$confirm('您确定要删除该部门吗???').then(async() => {
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartment()
        // console.log(res)
      }).catch(err => err)
    },
    close() {
      this.showDialog = false
    }
  }
}
</script>
